<template>
  <div id="app" style="width: 50%;margin-left: 25%">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-card class="box-card" style="min-height: 100%;min-width:100%;height: 100%;align-items: center">
      <el-form-item label="书名" prop="goodsName">
        <el-input v-model="ruleForm.goodsName"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
      </el-form-item>
      <el-form-item label="出版社" prop="pressName">
        <el-input v-model="ruleForm.pressName"></el-input>
      </el-form-item>

      <el-form-item label="包装类型" prop="packId">
        <el-select v-model="ruleForm.packId" placeholder="请选择">
          <el-option
              v-for="item in packNamesOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="类别" prop="categoryId">
        <el-select v-model="ruleForm.categoryId" placeholder="请选择">
          <el-option
              v-for="item in categorysOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item style="margin-left: 25%">
        <a :href="'/search?goodsName='+this.ruleForm.goodsName+'&author='+this.ruleForm.author+'&pressName='+this.ruleForm.pressName+'&packId='+this.ruleForm.packId+'&categoryId='+this.ruleForm.categoryId">
          <el-button type="primary" @click="submitForm()">搜索</el-button></a>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      </el-card>
    </el-form>

  </div>
</template>
<script>
export default {
  data() {
    return {
      packNamesOptions: [],
      categorysOptions: [],
      ruleForm: {
        goodsName: '',
        author: '',
        pressName: '',
        packId:[],
        categoryId:[]
      },
      rules: {
        goodsName: [
          {required: true, message: '请输入书名', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 1 到 25 个字符', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请输入作者', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        pressName: [
          {required: true, message: '请输入出版社', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    loadPacksList() {
      let url = 'http://localhost:8080/pack/allName';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.packNamesOptions = responseBody.data;
      });
    },
    loadcategorysList() {
      let url = 'http://localhost:8080/pmsCategory/allName';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.categorysOptions = responseBody.data;
      });
    },

    submitForm(formName) {
      location.href="/search"
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/goods/highSearch';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm, {arrayFormat: 'repeat'});
          console.log('formData = ' + formData);
          this.axios
              .get(url, formData).then((response) => {
            let responseBody = response.data;
            this.productArr = responseBody.data;
            console.log('responseBody = ');
            console.log(responseBody);
              this.resetForm(formName);
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadcategorysList();
    this.loadPacksList();
  }
}
</script>

<style>
* {
  margin: 0;
}

.app {
  color: #333;
  text-align: center;
}

p {
  margin-left: 0;
}

</style>